/* Copyright (C) 2009 Alvaro Morais <alvaromorais@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details at
 * http://www.gnu.org/copyleft/gpl.html
 */

#include "curseswindow.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

//list<CursesWindow*> cursesWindows;
int maxX , maxY;

CursesWindow::CursesWindow() :
        password("Hello"),
        host("localhost"),
        lastMessageFromServer(""),
        updateTime(1),
        lastTimeRead(0)
{
    //associateHandler();

    //cursesWindows.push_back(this);

    initscr();
    screenSupportColors = true; // (has_colors() == TRUE);
    getmaxyx(stdscr,maxY,maxX);
    halfdelay(1);
    noecho();

    keypad(stdscr, TRUE);


    refresh();


    if(screenSupportColors)
        initColors();
    drawMainWindow();
    waitInput();

}

CursesWindow::~CursesWindow()
{
    endwin();
    //cursesWindows.remove(this);

}

void CursesWindow::drawMainWindow()
{
    main_x = 0;
    main_y = 0;
    main_height = 10;
    main_width = getmaxx(stdscr);

    if(screenSupportColors)
        attron(COLOR_PAIR(3));
    main_window = newwin(main_height, main_width, main_y, main_x);
    box(main_window, 0, 0);
    if(screenSupportColors)
        attroff(COLOR_PAIR(3));
    writeHeaders();

    wrefresh(main_window);
}

void CursesWindow::writeHeaders()
{
    string BforcerString = "bforcer - ";
    BforcerString += PACKAGE_VERSION;
    string BForcerLogo = "May the brute force be with you.";

    string password_line = "Current Pasword: ";
    password_line += password;

    if(screenSupportColors)
        attron(COLOR_PAIR(2));
    mvprintw(1,main_width/2 - BforcerString.size() / 2, BforcerString.c_str());
    mvprintw(2,main_width/2 - BForcerLogo.size() / 2, BForcerLogo.c_str());

    if(screenSupportColors)
        attroff(COLOR_PAIR(2));
}


void CursesWindow::initColors()
{
    start_color();
    init_pair(1, COLOR_WHITE, COLOR_BLACK);
    init_pair(2, COLOR_RED, COLOR_BLACK);
    init_pair(3, COLOR_BLUE, COLOR_BLACK);
}

void CursesWindow::signalWindowResized()
{
    wborder(main_window, ' ', ' ', ' ',' ',' ',' ',' ',' ');
    wrefresh(main_window);
    delwin(main_window);
    refresh();
    drawMainWindow();
}


void CursesWindow::waitInput()
{
    int ch;
    while((ch = getch()) != KEY_F(1))
    {
        refresh();


        if(checkIfResized())
        {
            signalWindowResized();
        }
    }
}

bool CursesWindow::checkIfResized()
{

    int currentMaxX, currentMaxY;

    getmaxyx(stdscr, currentMaxY, currentMaxX);
    if(maxX != currentMaxX || maxY != currentMaxY)
    {
        maxX = currentMaxX;
        maxY = currentMaxY;
        return true;
    }
    return false;

}
/*
void associateHandler()
{
    static bool isAssociated = false;
    if(!isAssociated)
    {
         signal(SIGWINCH, resizeHandler);
         isAssociated = true;
    }
}

void resizeHandler(int sig)
{

    list<CursesWindow*>::iterator i;
    for (i=cursesWindows.begin(); i!= cursesWindows.end(); ++i) {

        (*i)->signalWindowResized();
    }
}

*/
